package com.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.text.TextPaint;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;


import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.SimpleFormatter;

import lib.common.R;

/**
 * TODO: document your custom view class.
 */
public class MyOrdersDetailTimeView extends TextView {



    public MyOrdersDetailTimeView(Context context) {
        super(context);
        init(null, 0);

    }

    public MyOrdersDetailTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(attrs, 0);
    }

    public MyOrdersDetailTimeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes

        if (null != attrs) {
            TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyOrdersDetailTimeView);

            if (ta != null) {


                // ColorGroupCircle = ta.getColor(R.styleable.timeline_ColorRadiusBig, getResources().getColor(android.R.color.holo_green_light));


                ta.recycle();
            }
        }
        else{


        }


    }


    MyCountTimer CountTimer;
    public void start(long timestamp){


        if(CountTimer == null)
        {
            long time = timestamp - System.currentTimeMillis();
            CountTimer = new MyCountTimer(time, 1000);
            CountTimer.start();
        }

    }


    // http://www.2cto.com/kf/201207/139551.html
    class MyCountTimer extends CountDownTimer
    {


        public MyCountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onTick(long l) {


            Time time_current = new Time();
            time_current.set(l);

            Time date_system_current = new Time();
            date_system_current.set(System.currentTimeMillis());


            int day = time_current.monthDay - date_system_current.monthDay;
            int hour = time_current.hour;
            int minutes = time_current.minute;
            int second = time_current.second;


            int _hour = 0;
            for(int i =0; i<day; i++){
                _hour +=i*24;
            }



            String str = hour+_hour + ":" + AddLefZero(""+minutes) + ":" + AddLefZero(""+second);

            setTextColor(getResources().getColor(android.R.color.white));
            setText(str);

        }

        @Override
        public void onFinish() {

            if(getVisibility() == View.VISIBLE){

            }

        }
    }

    String AddLefZero(String i){

        return i.length()<2 ? "0"+i : i;

    }


}
